package com.zy.algorithm.controller.screen;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.fragment.BaseAlgorithmFragment;
import com.zy.utils.ActivityUtils;
import com.zy.utils.AnimatorUtils;
import com.zy.zlog.ZLog;

import java.util.List;

public abstract class BaseScreenController implements IScreenController {
    private static final String TAG = "BaseScreenController";

    private Context context;

    public BaseScreenController(Context context) {
        this.context = context;
    }

    protected final void startStepAnimator(List<Animator> animatorList, BaseAlgorithmFragment.StepListener listener) {
        Animator animator = AnimatorUtils.getPlaySequentially(animatorList);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation, boolean isReverse) {
                super.onAnimationEnd(animation, isReverse);
                listener.nextStep();
            }
        });
        animator.start();
    }

    @Override
    public void startSubCartoon(List<SortStepBean> cartoonStepBeanList, BaseAlgorithmFragment.StepListener stepListener) {
        //设置当前操作位置
        //一步一步执行

        startPlay(cartoonStepBeanList, 0, stepListener);
    }

    private void startPlay(List<SortStepBean> cartoonStepBeanList, int index, BaseAlgorithmFragment.StepListener stepListener) {
        if (index >= cartoonStepBeanList.size()) {
            stepListener.nextStep();
            return;
        }

        SortStepBean bean = cartoonStepBeanList.get(index);
        ZLog.d(TAG, index + " :" + bean);

        setSortData(bean, true);
        //开始每一步的动画
        playStepAnimation(index, bean, new BaseAlgorithmFragment.StepListener() {
            @Override
            public void nextStep() {

                if (checkActivityDestroyed()) {
                    return;
                }
                statuesResetAll();
                setSortData(bean, false);
                startPlay(cartoonStepBeanList, index + 1, stepListener);
            }
        });
    }

    protected boolean checkActivityDestroyed() {
        return ActivityUtils.checkActivityDestroyed(context);
    }

    public void statuesResetAll() {
        View[] views = getDataViewS();
        for (View view : views) {
            view.setTranslationX(0);
            view.setTranslationY(0);
            view.setScaleX(1f);
            view.setScaleY(1f);
            view.setAlpha(1f);
            view.setVisibility(View.VISIBLE);
        }
    }

    abstract int getWH();

    abstract float getTextSize();

    public TextView buildDataTextView(int dataNum) {
        int WH = getWH();
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WH, WH);

        textView.setLayoutParams(layoutParams);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getTextSize());
        textView.setText("" + dataNum);
        textView.setTextColor(context.getResources().getColor(R.color.statues_text_color));
        textView.setBackgroundColor(context.getResources().getColor(R.color.statues_unsorted));
        return textView;
    }

    //占位View
    public View buildEmptyView() {
        View empty = new View(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 0, 1);
        empty.setLayoutParams(layoutParams);
        return empty;
    }

    protected abstract View[] getDataViewS();

    abstract void playStepAnimation(int index, SortStepBean bean, BaseAlgorithmFragment.StepListener stepListener);

    abstract View getDataViewByIndex(int index);
}
